<?php

namespace haibao\erp\business\product;

use \haibao\erp\data\product\ProductCategoryProperty as PropertyData;
use \haibao\erp\model\filter\product\ProductCategoryProperty as CategoryPropertyFilter;
use \haibao\erp\business\product\ProductCategory as CategoryBusiness;
use \haibao\frame\data\query\Condition;
use \haibao\erp\business\BusinessException;
use \haibao\erp\business\Base;

class ProductCategoryProperty extends \haibao\erp\business\Base{
	public  static $dataType = array(
			\haibao\erp\model\data\product\ProductCategoryProperty::DATA_TYPE_INPUT_TEXT =>'输入框',
			\haibao\erp\model\data\product\ProductCategoryProperty::DATA_TYPE_SELECT =>'下拉框',
			\haibao\erp\model\data\product\ProductCategoryProperty::DATA_TYPE_TEXTAREA =>'文本框',
			\haibao\erp\model\data\product\ProductCategoryProperty::DATA_TYPE_COIN =>'货币'
	);
	/**
	 * @param string $propertyName
	 * @param int $categoryId
	 * @param int $propertyId
	 */
	public static function hasSamePropertyName($propertyName,$categoryId,$propertyId = null){
		if(!$propertyName){
			throw new BusinessException('属性名称不存在',Base::DATA_EMPTY_NAME);
		}
		if(!$categoryId){
			throw new BusinessException('分类id不存在',Base::DATA_EMPTY_ID);
		}
		$propertyFilter = new CategoryPropertyFilter();
		if($propertyId){
			$propertyFilter->where(CategoryPropertyFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_NOTEQUAL, $propertyId);
		}
		$propertyFilter->where(CategoryPropertyFilter::CONDITION_FIELD_NAME_CATEGORY_ID, Condition::CONDITION_EQUAL, $categoryId);
		$propertyFilter->where(CategoryPropertyFilter::CONDITION_FIELD_NAME_PROPERTY_NAME, Condition::CONDITION_EQUAL, $propertyName);
		$propertyFilter->where(CategoryPropertyFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\erp\model\data\product\ProductCategoryProperty::STATUS_ENABLE);
		$data = new PropertyData();
		if($data->getAll($propertyFilter)){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 添加产品属性
	 * @param (array)\haibao\erp\model\data\ProductCategoryProperty $categoryPropertyModelArr
	 */
	public static function addProductCategoryProperty($categoryPropertyModel){
		self::checkOperation(self::FUNC_CODE_PRODUCT_PROPERTY_ADD);
		
		if(!$categoryPropertyModel && !($categoryPropertyModel instanceof \haibao\erp\model\data\product\ProductCategoryProperty)){
			throw new BusinessException('产品属性不存在',Base::DATA_EMPTY);
		}
		if(!$categoryPropertyModel->PropertyName){
			throw new BusinessException('属性名称不能为空',Base::DATA_EMPTY_NAME);
		}
		if(!$categoryPropertyModel->CategoryId){
			throw new BusinessException('分类不能为空',Base::DATA_EMPTY_ID);
		}
		if(self::hasSamePropertyName($categoryPropertyModel->PropertyName, $categoryPropertyModel->CategoryId)){
			throw  new BusinessException('此分类下属性名已存在',Base::DATA_ERROR_NAME_EXIST);
		}
		if(!$categoryPropertyModel->DataType){
			throw  new BusinessException('属性类型不能为空',Base::DATA_EMPTY);
		}
			
		$categoryPropertyModel->CreateBy = self::getCurrentUser()->Id;
		$categoryPropertyModel->CreateTime = new \DateTime();
		$categoryPropertyModel->Status = \haibao\erp\model\data\product\ProductCategory::STATUS_ENABLE;
			
		$categoryPropertyModel->OrderNo = 1;
		$data = new PropertyData();
		$data->add($categoryPropertyModel);
		
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_PROPERTY_ADD, $categoryPropertyModel->toArray());
	}
	
	/**
	 * 保存属性
	 */
	public static function updateCategoryProperty($categoryPropertyModel){
	    
		self::checkOperation(self::FUNC_CODE_PRODUCT_PROPERTY_ADD);
		
		if(!$categoryPropertyModel && !($categoryPropertyModel instanceof \haibao\erp\model\data\product\ProductCategoryProperty)){
			throw new BusinessException('产品属性不存在',Base::DATA_EMPTY);
		}
		if(!$categoryPropertyModel->Id){
			throw new BusinessException('属性id不能为空',Base::DATA_EMPTY_ID);
		}
		if(!$categoryPropertyModel->PropertyName){
			throw new BusinessException('属性名称不能为空',Base::DATA_EMPTY_NAME);
		}
		if(self::hasSamePropertyName($categoryPropertyModel->PropertyName, $categoryPropertyModel->CategoryId,$categoryPropertyModel->Id)){
			throw  new BusinessException('此分类下属性名已存在');
		}
		if(!$categoryPropertyModel->DataType){
			throw  new BusinessException('属性类型不能为空');
		}
		
		$data = new PropertyData();
		$data->save($categoryPropertyModel);
		
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_PROPERTY_ADD, $categoryPropertyModel->toArray());
	}
	/**
	 * 根据产品属性id查找
	 */
	public static function getPropertyById($propertyId){
		if(!$propertyId){
			throw new BusinessException('属性id不能为空',Base::DATA_EMPTY_ID);
		}
		$data = new PropertyData();
		return $data->getPropertyById($propertyId);
	}
	
	/**
	 * 获取当前分类下最大的排序
	 */
    public static function getCurrentMaxOrder($categoryId){
    	$data = new PropertyData();
    	$max = $data->getMaxOrder($categoryId);
    	return !$max ? \haibao\erp\model\data\product\ProductCategoryProperty::DEFAULT_START_ORDERNO :$max + 1;
    }
    
    /**
     * 设置子分类的属性
     */
    public static function setCategoryProperty($categoryPropertyModel,$isAdd = true){
    	if($categoryPropertyModel->SnycSub == \haibao\erp\model\data\product\ProductCategoryProperty::IS_SYNC_SUB){
    		$childCategoryIds = CategoryBusiness::getAllChildrenCategory($categoryPropertyModel->CategoryId);
    		$subCategoryProperty = array();
    		if($childCategoryIds){
    			foreach($childCategoryIds as $key=>$childCategory){
    				if(!self::hasSamePropertyName($categoryPropertyModel->PropertyName,$childCategory['Id'])){
    					$propertyModel = clone $categoryPropertyModel;
    					if(!$isAdd){
    						$categoryPropertyModel->CreateBy = self::getCurrentUser()->Id;
    						$categoryPropertyModel->CreateTime = new \DateTime();
    						$categoryPropertyModel->Status = \haibao\erp\model\data\product\ProductCategory::STATUS_ENABLE;
    					}
    					$propertyModel->CategoryId = $childCategory['Id'];
    					$propertyModel->OrderNo = self::getCurrentMaxOrder($childCategory['Id']);
    					array_push($subCategoryProperty, $propertyModel);
    				}
    			}
    		}
    		$categoryPropertyModel->SubCategoryProperty = $subCategoryProperty ;
    		return $categoryPropertyModel;
    	}
    }
    public static function getPropertyByFilter($filter){
    	$data = new PropertyData();
    	return $data->getAll($filter);
    }
    
    /**
	 * 删除属性
	 */
	public static function deletePropertyById($propertyId){
		self::checkOperation(self::FUNC_CODE_PRODUCT_PROPERTY_DEL);
	
		if(!$propertyId){
			throw new BusinessException('属性不能为空',self::DATA_EMPTY_ID);
		}
		$data = new PropertyData();
	
		$arrSet = array();
		$arrSet['Status']  = \haibao\erp\model\data\product\ProductCategoryProperty::STATUS_DISABLE;
		$data->deleteProperty($propertyId,$arrSet);
	
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_PROPERTY_DEL, array('Id' =>$propertyId));
	} 
}